<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `1,2,8,7`
        let arr = []
        let list = input.split(',').map(v => parseInt(v))
        let Tag = true
        list.forEach((t) => {
            if (t < 0 || t > 10) {
                Tag = false
            } else {
                if (t === 2) {
                    arr.push(2)
                    arr.push(5)

                } else if (t === 5) {
                    arr.push(2)
                    arr.push(5)
                }
                else if (t === 6) {
                    arr.push(6)
                    arr.push(9)
                }
                else if (t === 9) {
                    arr.push(6)
                    arr.push(9)
                } else arr.push(t)
            }

        })

        let res = []


        function dps(arr, str, visited = new Set()) {

            let visited_now=new Set([...visited])        
            
            for (let i = 0; i < arr.length; i++) {
                if (visited_now.has(arr[i])) {
                    
                    continue
                } else {
                    if (arr[i] === 2 || arr[i] === 5) {
                        visited_now.add(2)
                        visited_now.add(5)
                        let t = str + arr[i]
                        res.push(parseInt(t))
                        dps(arr,t,visited_now)
                        visited_now=new Set([...visited]) 
                    } else 
                    if (arr[i] === 6 || arr[i] === 9) {
                        visited_now.add(6)
                        visited_now.add(9)
                        let t = str + arr[i]
                        res.push(parseInt(t))
                        dps(arr,t,visited_now)
                        visited_now=new Set([...visited]) 
                    } else 
                    {
                        visited_now.add(arr[i])
                        let t = str + arr[i]
                        res.push(parseInt(t))
                        dps(arr,t,visited_now)
                        visited_now=new Set([...visited]) 
                    }

                }


            }

        }



        if ([...new Set(arr)].length != arr.length || !Tag) {
            console.log(-1);

        } else {
            let max = Math.max(...list)

            dps(arr, '')
            console.log(res);            

            console.log(res.sort((a,b)=>a-b));
  

        }







    </script>
</body>

</html>